﻿using NLog;
using NLog.Config;
using System;
using System.Diagnostics;

namespace QQTools.Logging {
    //public sealed class LogManager {
    //    private static readonly Logger logger = NLog.LogManager.GetCurrentClassLogger(true);// NLog.LogManager.GetCurrentClassLogger(true);

    //    private LogManager() { }

    //    public static void Info(string message) => logger.Info(message);
    //    public static void Info(string message, Exception err) => logger.Info(message, err);
    //    public static void Error(string message) => logger.Error(message);
    //    public static void Error(string message, Exception err) => logger.Error(message, err);
    //    public static void Debug(string message) => logger.Debug(message);
    //    public static void Debug(string message, Exception err) => logger.Debug(message, err);
    //    public static void Warn(string message) => logger.Warn(message);
    //    public static void Warn(string message, Exception err) => logger.Warn(message, err);
    //}

    public sealed class LogManager
    {
        private static readonly Logger logger = NLog.LogManager.GetCurrentClassLogger();

        private LogManager() { }

        public static void Info(string message) => Log(LogLevel.Info, message);
        public static void Info(string message, Exception err) => Log(LogLevel.Info, message, err);
        public static void Error(string message) => Log(LogLevel.Error, message);
        public static void Error(string message, Exception err) => Log(LogLevel.Error, message, err);
        public static void Debug(string message) => Log(LogLevel.Debug, message);
        public static void Debug(string message, Exception err) => Log(LogLevel.Debug, message, err);
        public static void Warn(string message) => Log(LogLevel.Warn, message);
        public static void Warn(string message, Exception err) => Log(LogLevel.Warn, message, err);

        private static void Log(LogLevel level, string message, Exception err = null)
        {
            var logEvent = new LogEventInfo(level, logger.Name, message) { Exception = err };
            var callerType = new StackFrame(2).GetMethod().DeclaringType;
            logger.Log(callerType, logEvent);
        }
    }
}
